package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JLabel;

import common.URIFormatter;


public class BrokerShutdown extends javax.swing.JDialog {

	private static final long serialVersionUID = 1L;
	private javax.swing.JButton buttonResume;
	private javax.swing.JLabel labelDescription;
	private javax.swing.JProgressBar progressBarStatus;

	public BrokerShutdown(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
		setCenterLocation();
	}

	private void buttonResumeActionPerformed(java.awt.event.ActionEvent evt) {
		ClientGuiLauncher.clientFrame
				.getPanelIP()
				.getTextFieldBrokerIP()
				.setText(
						URIFormatter
								.format(ClientGuiLauncher.clientFrame.client
										.getMyBroker().uri));
		ClientGuiLauncher.clientFrame.setEnabled(true);
		this.dispose();
	}

	public JLabel getLabelDescription() {
		return labelDescription;
	}

	private void initComponents() {

		labelDescription = new javax.swing.JLabel();
		progressBarStatus = new javax.swing.JProgressBar();
		buttonResume = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
		setResizable(false);

		labelDescription
				.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		labelDescription
				.setText("Old broker disconnected, waiting for new connection");

		buttonResume.setText("Resume");
		buttonResume.setEnabled(false);
		buttonResume.setMargin(new java.awt.Insets(2, 5, 2, 5));
		buttonResume.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buttonResumeActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING)
												.addComponent(
														progressBarStatus,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														257, Short.MAX_VALUE)
												.addComponent(
														labelDescription,
														javax.swing.GroupLayout.Alignment.LEADING,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE))
								.addContainerGap())
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addGap(106, 106, 106)
								.addComponent(buttonResume,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE).addGap(120, 120, 120)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(labelDescription)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(progressBarStatus,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.addComponent(buttonResume).addContainerGap()));

		pack();
	}

	private void setCenterLocation() {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension size = this.getSize();
		int x = (screenSize.width - size.width) / 2;
		int y = (screenSize.height - size.height) / 2;
		this.setLocation(x, y);
	}

	public void setProgress(int progress) {
		progressBarStatus.setValue(progress);
		if (progress <= 25)
			progressBarStatus.setForeground(Color.RED);
		else if (progress > 25 && progress <= 50)
			progressBarStatus.setForeground(Color.ORANGE);
		else if (progress > 50 && progress <= 75)
			progressBarStatus.setForeground(Color.YELLOW);
		else
			progressBarStatus.setForeground(Color.GREEN);
		progressBarStatus.repaint();
		if (progress >= 100)
			buttonResume.setEnabled(true);
	}
}
